package deng.jboss.examples.webservices;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

/**
 * Hello program.
 * 
 * @author Zemian Deng
 */

@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC)
public class HelloSubject2 {
	
	private static Logger logger = Logger.getLogger(HelloSubject2.class);
	
	@WebMethod
	public void addSubject(@WebParam(name="subject") Subject2 subject) throws Exception {
		logger.info("Not yet implemented: I suppose to save subject " + subject);
	}
	
	@WebMethod
	public Subject2 getSubject(@WebParam(name="subjectId") Long subjectId) throws Exception {
		SimpleDateFormat df = new SimpleDateFormat("MMddyyyy");
		Subject2 s = new Subject2();
		s.setId(subjectId);
		s.setFirstName("John");
		s.setLastName("Smith");
		s.setDateOfBirth(df.parse("01012010"));
		s.setImageType("gif");
		s.setImage(loadJBossLogoImage());
		return s;
	}
	
	private byte[] loadJBossLogoImage() throws IOException {
		String res = "images/jboss-logo.gif";
		InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(res);
		if (input == null) {
			throw new RuntimeException("Failed to load image classpath:" + res);
		}
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		IOUtils.copy(input, output);
		return output.toByteArray();
	}

	@WebMethod
	public byte[] getSubjectImage(@WebParam(name="subjectId") Long subjectId) throws Exception {
		byte[] ret = new byte[] { 0x00, 0x01, 0x02, (byte)0xFF };
		return ret;
	}
	
}